package com.ivanceras.client.widgets;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.ivanceras.client.DataService;
import com.ivanceras.client.DataServiceAsync;
import com.ivanceras.shared.bo.Ad_column;

public class FieldController {
	
	private final DataServiceAsync dataService = GWT
	.create(DataService.class);
	
	List<String> dataIds = null;
	HashMap<String, String> listData = null;
	List<String> dataNames = null;

	
	public FieldController(AdFieldUi adFieldUi) {
		// TODO Auto-generated constructor stub
	}


	public void retrieveDataName(Ad_column column, String value, final AsyncCallback<String> callback) {
		dataService.retrieveDataName(column, value, callback);
	}
	

	public void retrieveListData(Ad_column column, final AsyncCallback<HashMap<String, String>> callback){
		dataService.retrieveListData(column, new AsyncCallback<HashMap<String,String>>() {


			@Override
			public void onSuccess(HashMap<String, String> result) {
				listData = new HashMap<String, String>();
				listData.put("-1", " ");
				listData.putAll(result);
				dataIds = new ArrayList<String>();
				dataNames = new ArrayList<String>();
				dataIds.add(null);
				dataIds.add(" ");
				if(result != null){
					for(Entry<String, String> set : result.entrySet()){
						String id = set.getKey();
						String name = set.getValue();
						dataIds.add(id);
						dataNames.add(name);
					}
				}
				callback.onSuccess(listData);
			}

			@Override
			public void onFailure(Throwable caught) {
				caught.printStackTrace();
				callback.onFailure(caught);
			}
		});

	}

}
